package com.weavernorth.updateKaoQin;

import com.engine.kq.biz.KQFormatData;
import com.weaver.general.Util;
import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.List;
import java.util.Map;

import static com.weavernorth.util.dateUtils.*;

/**
 * @Author: BruseeWang
 * @Date: 2021/3/5 13:47
 * @Description: 刷新考勤报表
 */
public class ZrefreshKaoQinTimmer extends BaseCronJob {
    public Logger log = Logger4jUtil.getLoggerByName("/Timmer/refreshKaoQinTimmer/");
    private String gh;
    private String begindate;
    private String endate;
    public String getGh() {
        return gh;
    }
    public void setGh(String gh) {
        this.gh = gh;
    }
    public String getBegindate() {
        return begindate;
    }
    public void setBegindate(String begindate) {
        this.begindate = begindate;
    }
    public String getEndate() {
        return endate;
    }
    public void setEndate(String endate) {
        this.endate = endate;
    }

    @Override
    public void execute() {
        log.info("刷新考勤报表定时任务开始=====>");
        //刷新考勤汇总报表
        String strYesterday = DateUtil.getYesterday();
        String strBegin="";
        String strEnd="";
        //获取当前日期
        String nowDate = getYearMonthDay();
        String year=nowDate.split("-")[0];
        String month=nowDate.split("-")[1];
        //获取当前日期天数
        String day= nowDate.split("-")[2];
        if (Integer.parseInt(day)>4){//当前日期在4号之后,取本月
            log.info("当前日期在4号之后时：");
            strBegin=year+"-"+month+"-01";
            log.info("开始日期："+strBegin);
            strEnd=nowDate;
            log.info("结束日期："+strEnd);
        }else{
            strBegin=getPreMonthFirstDate();//获取上个月第一天
            log.info("开始日期："+strBegin);
            strEnd=nowDate;
            log.info("结束日期："+strEnd);
        }
        if(begindate!=null){
            if(isValidDate(begindate,"yyyy-MM-dd")){
                strBegin=begindate;
                log.info("日期参数-开始日期："+strBegin);
            }
        }
        if(endate!=null){
            if(isValidDate(endate,"yyyy-MM-dd")) {
                strEnd = endate;
                log.info("日期参数-结束日期："+strEnd);
            }
        }
        if("".equals(strBegin)){
            strBegin=strYesterday;
        }
        if("".equals(strEnd)){
            strEnd=strYesterday;
        }
        //两个日期比大小，开始日期大于结束日期时要进行交换
        String strTemp="";
        if(strBegin.compareTo(strEnd)>0){
            strTemp=strBegin;
            strBegin=strEnd;
            strEnd=strTemp;
        }
        RecordSet rs=new RecordSet();
        //取所有人
//        String strQuery="SELECT id FROM HRMRESOURCE WHERE STATUS=1 AND WORKCODE is not NULL";
//        if(gh!=null){
//            strQuery+=" and workcode='"+gh+"'";
//        }
        //只取考勤异常的人员
        String strQuery="SELECT\n" +
                "\tRESOURCEID,\n" +
                "\tKQDATE \n" +
                "FROM\n" +
                "\tkq_format_detail a \n" +
                "WHERE\n" +
                "\t a.kqdate >= '"+strBegin+"' \n" +
                "\tAND a.kqdate <= '"+strEnd+"' \n" +
                "\tAND a.KQDATE NOT IN (\n" +
                "\tSELECT\n" +
                "\t\tKQDATE \n" +
                "\tFROM\n" +
                "\t\toauser.kq_format_detail \n" +
                "\tWHERE\n" +
                "\t\tRESOURCEid = a.RESOURCEid \n" +
                "\t\tAND kqdate = a.kqdate \n" +
                "\t\tAND belatemins = 0 \n" +
                "\t\tAND graveBeLateMins = 0 \n" +
                "\t\tAND leaveearlymins = 0 \n" +
                "\t\tAND graveLeaveEarlyMins = 0 \n" +
                "\t\tAND absenteeismmins = 0 \n" +
                "\t\tAND LEAVEMINS = 0 \n" +
                "\t\tAND EVECTIONMINS = 0 \n" +
                "\tAND FORGOTCHECKMINS = 0)";
        //todo 去除公出条件：OUTMINS = 0,待发布 2022-02-01
        log.info("sql语句："+strQuery);
        rs.execute(strQuery);
        Integer rows=0;
        String strUserid;
        String strKqdate;
        while (rs.next()){
            strUserid = rs.getString("RESOURCEID");
            log.info("人员id:"+strUserid);
            strKqdate = rs.getString("kqdate");
            log.info("考勤日期："+strKqdate);
            Map<String, Object> stringObjectMap = new KQFormatData().formatKqDate(strUserid, strKqdate);
            rows++;
        }
        log.info("受影响行数："+rows);
//        List<String> everyday = getEveryday(strBegin, strEnd);//获取日期段每一天
//        log.info("日期集合："+everyday.toString());
//        for (String s : everyday) {
//          while (rs.next()) {
//              String strUserid = rs.getString("id");
//              Map<String, Object> stringObjectMap = new KQFormatData().formatKqDate(strUserid, s);
//              rows++;
//          }
//        }
        log.info("定时任务执行结束,受影响行数："+rows);
    }
}
